﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;
using XingLucifer.IBase;
using UIWindows.Domains;
using UIWindows.GlobalStatics;
using UIWindows.Tools;

namespace UIWindows.Services
{
    public class ScanCodeHook
    {
        #region 单例模式
        private static readonly object _lock = new object();
        private static ScanCodeHook _codeHook;
        public static ScanCodeHook GetObject
        {
            get
            {
                if (_codeHook == null)
                {
                    lock (_lock)
                    {
                        if (_codeHook == null)
                        {
                            _codeHook = new ScanCodeHook();
                        }
                    }
                }
                return _codeHook;
            }
        }
        #endregion

        private ScanCodeHook()
        {
            _systemConfig = ObjectContainer.ResolveSingleton<SystemConfigStatic>();
            KeyboardHook.RegistrationListening(12, ScanCodeHandle);
        }

        private SystemConfigStatic _systemConfig;
        
        public void ScanCodeHandle(string code)
        {
            $"手持扫码反馈条码==>>{code}".RunLog(MessageLevelType.信息);
            string barcode = System.Text.RegularExpressions.Regex.Match(code, _systemConfig.SCM.BarcodeRules, System.Text.RegularExpressions.RegexOptions.Compiled).Value;
            if (!string.IsNullOrEmpty(barcode))
            {
                $"手持扫码返回条码校验不通过!!! 校验后的条码==>>{barcode}".RunLog(MessageLevelType.信息);
                return;
            }

        }

        private bool WriteInjectionVolume(float value_inj, IDevices devices, int address, string method)
        {
            for (int i = 1; i < 4; i++)
            {
                if (devices.WriteSingle(value_inj, address + (method == "字节" ? 12 : 6), 0, 0))
                {
                    $"第[{i}]次 注液量 [{value_inj:F2}] 写入成功".RunLog(MessageLevelType.信息);
                    return true;
                }
                else
                {
                    $"第[{i}]次 注液量 [{value_inj:F2}] 写入失败".RunLog(MessageLevelType.警告);
                }
            }
            return false;
        }
    }
}
